import mx.binding.utils.BindingUtils;
import mx.collections.ArrayCollection;

import model.GameModelLocator;
import model.vo.GameVo;

[Bindable]
private var gameVo:GameVo = GameModelLocator.getInstance().gameVo;
[Bindable]
private var gameTimer:GameTimer;
private var preScore:int;
private var smallEggList:ArrayCollection = new ArrayCollection();

private function onCreationComplete():void {
    this.gameTimer = new GameTimer(Config.timeLimit);
    this.timeBar.redraw(this.gameTimer.currentTime);
    BindingUtils.bindSetter(setScore, gameVo, "score");
    BindingUtils.bindSetter(setArrive, gameVo, "arrive");
    BindingUtils.bindSetter(setCurrentTime, this.gameTimer, "currentTime");
}

private function setCurrentTime(time:int):void {
    this.timeBar.redraw(time);
    if (time <= 0) {
        resultGame();
    } 
    if (time <= Config.emergencyTime) {
        SoundLocator.playSound(SoundLocator.SOUND_TICK);
    }
}

private function setScore(score:int):void {
    if (score <= 0 || gameVo.state != Constants.GAME_STATE.RUNNING) return;
    for each (var s:int in gameVo.scoreRules) {
        if (preScore < s && score >= s) {
            gameVo.eggs += 1;
            giveAnEgg();
        }
    }
    trace("Event: EGG");
    preScore = score;
}

private function setArrive(arrive:Boolean):void {
    if(arrive) resultGame();
}

private function giveAnEgg():void {
    var egg:SmallEgg = new SmallEgg();
    egg.x = 255;
    egg.y = 217;
    var e:DisplayObject = this.scoreEggList.getChildByName(String(gameVo.eggs));
    var p:Point = new Point(e.x, e.y);
    p = this.scoreEggList.localToGlobal(p);
    // Here may be need to change little to make the egg move to the extract place
    egg.xTo = p.x - 10;
    egg.yTo = p.y - 9;
    this.addChild(egg);
    egg.moveEgg.play();
    SoundLocator.playSound(SoundLocator.SOUND_GET_EGG);
    this.smallEggList.addItem(egg);
}

// Play game
public function play():void {
    if (gameVo.state != Constants.GAME_STATE.PREPARING) return;
    for each (var egg:SmallEgg in this.smallEggList) {
        this.removeChild(egg);
    }
    this.smallEggList.removeAll();
    this.nextLevel.visible = false;
    this.eggList.visible = false;
    this.nextLevel.currentState = "";
    gameTimer.currentTime = Config.timeLimit;
    gameVo.balance -= 5;
    gameVo.reset();
    // reset gameView
    this.gameView.reset();
    this.levelImg.visible = true;
}

// After levelImg zoom, will start game
private function startGame():void {
    if (gameVo.state != Constants.GAME_STATE.PREPARING) return;
    this.levelImg.visible = false;
    gameTimer.startCount();
    gameVo.state = Constants.GAME_STATE.RUNNING;
    preScore = 0;
}

public function resultGame():void {
    if (gameVo.state != Constants.GAME_STATE.RUNNING) return;
    gameVo.state = Constants.GAME_STATE.RESULTING;
    this.gameTimer.stop();
    this.gameView.stop();
    this.showEggs();
}

private function showEggs():void {
    if (gameVo.state == Constants.GAME_STATE.RESULTING) {
        this.eggList.showEggs();
        this.eggList.visible = true;
    }
}
